package org.flown.novel.controller;


import jakarta.servlet.http.HttpSession;
import org.flown.novel.data.Author;
import org.flown.novel.data.User;
import org.flown.novel.service.AuthorService;
import org.flown.novel.service.ServiceResult;
import org.flown.novel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@RequestMapping("/author")
@Controller
public class AuthorController
{
    @Autowired
    private UserService service_user;
    @Autowired
    private AuthorService service_author;


    @ResponseBody
    @RequestMapping("/getAuthorByID")
    public Author getAuthorByID(HttpSession session, @RequestParam("id") int author_id)
    {
        return this.service_author.getAuthorByID(author_id);
    }

    @ResponseBody
    @RequestMapping("/getAuthorByUserID")
    public Author getAuthorByUserID(HttpSession session, @RequestParam("id") int user_id)
    {
        return this.service_author.getAuthorByUserID(user_id);
    }

    @ResponseBody
    @RequestMapping("/getAuthorByName")
    public Author getAuthorByName(HttpSession session, @RequestParam("name") String name)
    {
        return this.service_author.getAuthorByName(name);
    }


    /**
     * 登录了user才可用，登录了author不可用
     * */
    @ResponseBody
    @PostMapping("/register")
    public String register(HttpSession session, @RequestParam String pen_name)
    {
        if (session.getAttribute(SessionAttribute.AUTHOR.getName()) != null)
            return "你已经是作者了";

        User user = (User)session.getAttribute("user");
        if (user == null)
            return "注册失败";
        ServiceResult result = this.service_author.register(user.getId(), pen_name);
        return switch (result) {
            case SUCCESS -> "注册成功";
            case REGISTERED -> "你已经是作者了";
            case EXISTS_NAME -> "该笔名已被使用";
            case NULL_NAME -> "笔名为空";
            default -> "注册失败";
        };
    }


    @ResponseBody
    @PostMapping("/login")
    public String login(HttpSession session, User user)
    {
        ServiceResult result = service_user.login(user.getUsername(), user.getPassword());
        return switch (result)
        {
            case SUCCESS -> {
                User original_user = service_user.getUserByUsername(user.getUsername());
                Author author = this.service_author.getAuthorByUserID(original_user.getId());
                if (author == null)
                    yield "该账号未注册作者";
                else
                {
                    //session.setAttribute("user", original_user);
                    session.setAttribute("author", author);
                    yield "登录成功";
                }
            }
            case NULL_USERNAME -> "用户名为空";
            case NULL_PASSWORD -> "密码为空";
            case NOT_EXISTS_USER -> "用户不存在";
            default -> "登录失败";
        };
    }



}


